<?php
/**
 * 景点
 */
namespace App\Http\Controllers\Admin\Finance;



use App\Http\Controllers\Admin\Finance\Service\Withdraw;
use App\Http\Controllers\Admin\Finance\Repository\Withdraw as WithdrawRepository;
use App\Http\Controllers\Admin\Member\Service\Member;
use App\Setting;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class WithdrawController extends Controller
{
    private $manager;
    private $repository;
    private $request;
    private $viewPrefix = 'admin.finance.withdraw.';
    private $routePrefix = 'admin.finance.withdraw';
    public function __construct (Request $request)
    {
        $this->manager = new Withdraw();
        $this->repository = new WithdrawRepository();
        $this->request = $request;
    }

    /**
     * 文章列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(){
        if(isset($this->request->act)){
            $act = $this->request->act;
            return $this->{$act}();
        }else{

            return view($this->viewPrefix.'index',[
                'manager'   =>  $this->manager,
                'routePrefix'   =>  $this->routePrefix,
                'status'    =>  $this->manager->getStatusList()
            ]);
        }

    }
    public function add(){
        return view($this->viewPrefix.'add-edit',[
            'manager'   =>  $this->manager,
            'routePrefix'   =>  $this->routePrefix
        ]);
    }

    /**
     * 保存或插入
     * @return \Illuminate\Http\RedirectResponse
     */
    public function saveOrInsert(){
        $this->validate($this->request,[
            'member_id'    =>  'required|exists:members,id',
        ]);
        $memberModel = new Member();
        $model = $memberModel->getMember($this->request->member_id);
        $this->validate($this->request,[
            'price'    =>  'required|between:1,'.$model->wallet_push_money,
        ]);
        $this->manager->withdraw($this->request->post());
        return redirect()->route($this->routePrefix)->with(
            $this->success()
        );
    }

    /**
     * ajax列表
     * @return array
     */
    public function indexAjax(){

        return $this->page_json_render($this->manager->getPageList($this->request));
    }
    public function memberList(){
        $comboManager = new Member();
        return view($this->routePrefix.'.memberList',[
            'status' =>    $comboManager->getStatusList(),
            'manager'   =>  $comboManager,
            'routePrefix'   =>  'admin.member.member',
        ]);
    }
    public function read($id){
        $model  = $this->manager->getWithdraw($id);
        $model->status_name = $this->manager->getStatusString($model->status);
        $model->type_name = $this->manager->getTypeString($model->type);
        return view($this->viewPrefix.'read',[
            'manager'   =>  $this->manager,
            'routePrefix'   =>  $this->routePrefix,
            'model' =>  $model,
        ]);
    }

    private function apply(){
        $res = $this->manager->setApprove($this->request->post('id'),$this->request->post('desc'));
        if($res&&!isset($res['error'])) return ['code'=>'success'];
        else return ['code'=>'error','msg'=>$res['msg']];
    }
    private function pay(){
        $this->manager->setPay($this->request->post('id'),$this->request->post('desc'));
        return ['info'=>'success'];
    }
    private function refuse(){
        $this->manager->refuse($this->request->post('id'),$this->request->post('desc'));
        return ['info'=>'success'];
    }
}
